//
//  ScreenBrightnessViewController.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/17.
//

import UIKit
import SVProgressHUD

class ScreenBrightnessViewController: RViewController {

    @IBOutlet weak var tableView: UITableView!
    let data = ["低", "中", "高"]
    var brightness = ScreenBrightTool.getScreenBrightness()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = hexColor("F9F9F9")
        self.tableView.register(UINib.init(nibName: "ScreenTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "ScreenTableViewCell")
        self.tableView.layer.cornerRadius = 8
        self.tableView.separatorStyle = .none
        self.tableView.isScrollEnabled = false
        self.tableView.rowHeight = ScreenRowHeight
        if let info = RManager.manager.info {
            VTO2Communicate.sharedInstance().delegate = self
        }
        self.tableView.reloadData()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        SVProgressHUD.dismiss()
    }

}

extension ScreenBrightnessViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ScreenTableViewCell") as! ScreenTableViewCell
        cell.selectionStyle = .none
        let str = data[indexPath.row]
        cell.titleLabel.text = str
        cell.separLine.isHidden = indexPath.row == data.count - 1
        cell.selectIcon.image = UIImage.init(named: str == self.brightness ? "select" : "disSelect")
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 2-High    1-Medium     0-Low
        self.setDeviceBri(content: String(indexPath.row))
        self.brightness = self.data[indexPath.row]
    }
    
    func setDeviceBri(content: String) {
        if RManager.manager.currentDevice != nil {
            RManager.manager.info?.lightStrength = content
            SVProgressHUD.show()
            VTO2Communicate.sharedInstance().begin(to: VTParamTypeLightStrength, content: content)
        }
        self.tableView.reloadData()
    }
}

extension ScreenBrightnessViewController: VTO2CommunicateDelegate {
    
    func commonResponse(_ cmdType: VTCmdType, andResult result: VTCommonResult) {
        SVProgressHUD.dismiss()
    }
    
}
